var BattleServer = {
	Manage: {
		addWindowContainer: '',
		modifyWindowContainer: '',
		
		init: function() {
			Language.getCurrLanguage();
			Utils.loadJavascriptFile('/application/views/libraries/component/' + language + '.js');
			Utils.loadJavascriptFile('/application/views/libraries/utils/' + language + '.js');
			Utils.loadJavascriptFile('/application/views/module/BattleServer/' + language + '.js');
			Utils.initAjax();
			
			BattleServer.Manage.AddWindow.init();
			BattleServer.Manage.ModifyWindow.init();
			BattleServer.Manage.View.init();
		},
		
		View: {
			dataGridPanelContainer: '',
			
			init: function() {
				BattleServer.Manage.View.dataGridPanelContainer = $('#dataGridPanel #dataGrid');
				BattleServer.Manage.View.DataGrid.init();
			},
			
			DataGrid: {
				init: function() {
					BattleServer.Manage.View.DataGrid.create();
				},
				
				create: function() {
					var width = $(document).width() - 40;
					var height = $(document).height() - 40;
					BattleServer.Manage.View.dataGridPanelContainer.width(width);
					BattleServer.Manage.View.dataGridPanelContainer.height(height);
					BattleServer.Manage.View.dataGridPanelContainer.datagrid({
						url: '/index.php/BattleServer/manage/getListData',
						idField: "battleServerId",
						toolbar: [
							{
								iconCls: 'icon-add',
								text: battleServerManageViewDataGridToolbarButtonText1,
								handler: BattleServer.Manage.AddWindow.open
							},
							{
								iconCls: 'icon-edit',
								text: battleServerManageViewDataGridToolbarButtonText2,
								handler: BattleServer.Manage.ModifyWindow.open
								
							},
							{
								iconCls: 'icon-print',
								text: battleServerManageViewDataGridToolbarButtonText3,
								handler: BattleServer.Manage.View.DataGrid.doInstall
							}
						],
						columns: [[
							{
								checkbox: true
							},
							{
								field: 'operatorName',
								title: battleServerManageViewDataGridColumnTitle1,
								width: 80,
							},
							{
								field: 'areaNum',
								title: battleServerManageViewDataGridColumnTitle2,
								width: 60,
							},
							{
								field: 'serverName',
								title: battleServerManageViewDataGridColumnTitle3,
								width: 250,
							},
							{
								field: 'portGroup',
								title: battleServerManageViewDataGridColumnTitle4,
								width: 60,
							},
							{
								field: 'directoryName',
								title: battleServerManageViewDataGridColumnTitle5,
								width: 150,
							},
							{
								field: 'version',
								title: battleServerManageViewDataGridColumnTitle6,
								width: 100,
							},
							{
								field: 'status',
								title: battleServerManageViewDataGridColumnTitle7,
								width: 80,
								formatter: function(value) {
									if(value === '1') {
										return battleServerStatusValue1;
									}
									else if(value === '2') {
										return battleServerStatusValue2;
									}
									else if(value === '3') {
										return battleServerStatusValue3;
									}
									else if(value === '4') {
										return battleServerStatusValue4;
									}
								}
							},
						]],
						loadFilter: Utils.dataGridLoadFilter,
						pagination: true,
						pageSize: 50,
						pageList: [50, 100, 150, 200],
					})
				},
				
				load: function() {
					BattleServer.Manage.View.dataGridPanelContainer.datagrid('reload');
				},
				
				getCheckedRows: function(allowMulti) {
					var allowMulti = arguments[0] ? arguments[0] : false;
					var checkedRow = BattleServer.Manage.View.dataGridPanelContainer.datagrid('getChecked');
					
					if(Utils.dataGridValidateCheckedRow(checkedRow, allowMulti) === true) {
						return checkedRow;
					}
					else {
						return false;
					}
				},
				
				doInstall: function() {
					var checkedRow = BattleServer.Manage.View.DataGrid.getCheckedRows();
					
					if( !(checkedRow === false) ) {
						$.messager.confirm(
							battleServerManageConfirmInstallWindowTitle,
							battleServerManageConfirmInstallWindowContent,
							function(r) {
								if(r === true) {
									$.ajax({
										url: '/index.php/BattleServer/manage/install',
										data: {
											battleServerId: checkedRow[0].battleServerId
										},
									})
									.done(function(response) {
										if(response.errorCode === 0) {
											$.messager.alert(messageWindowTitle, battleServerManageDoInstallSuccessInfo, 'info');
										}
									});
								}
							}
						);
					}
				},
			},
		},
		
		AddWindow: {
			operatorLabelContainer: '',
			operatorInputContainer: '',
			areaNumLabelContainer: '',
			areaNumInputContainer: '',
			serverLabelContainer: '',
			serverInputContainer: '',
			portGroupLabelContainer: '',
			portGroupInputContainer: '',
			directoryNameLabelContainer: '',
			directoryNameInputContainer: '',
			versionLabelContainer: '',
			versionInputContainer: '',
			submitButtonContainer: '',
			resetButtonContainer: '',
			
			init: function() {
				BattleServer.Manage.addWindowContainer = $('#addWindow');
				BattleServer.Manage.AddWindow.operatorLabelContainer = $('#addWindow #operatorLabel');
				BattleServer.Manage.AddWindow.operatorInputContainer = $('#addWindow #operatorInput');
				BattleServer.Manage.AddWindow.areaNumLabelContainer = $('#addWindow #areaNumLabel');
				BattleServer.Manage.AddWindow.areaNumInputContainer = $('#addWindow #areaNumInput');
				BattleServer.Manage.AddWindow.serverLabelContainer = $('#addWindow #serverLabel');
				BattleServer.Manage.AddWindow.serverInputContainer = $('#addWindow #serverInput');
				BattleServer.Manage.AddWindow.portGroupLabelContainer = $('#addWindow #portGroupLabel');
				BattleServer.Manage.AddWindow.portGroupInputContainer = $('#addWindow #portGroupInput');
				BattleServer.Manage.AddWindow.directoryNameLabelContainer = $('#addWindow #directoryNameLabel');
				BattleServer.Manage.AddWindow.directoryNameInputContainer = $('#addWindow #directoryNameInput');
				BattleServer.Manage.AddWindow.versionLabelContainer = $('#addWindow #versionLabel');
				BattleServer.Manage.AddWindow.versionInputContainer = $('#addWindow #versionInput');
				BattleServer.Manage.AddWindow.submitButtonContainer = $('#addWindow #submitButton');
				BattleServer.Manage.AddWindow.resetButtonContainer = $('#addWindow #resetButton');
				BattleServer.Manage.AddWindow.create();
			},
			
			create: function() {
				BattleServer.Manage.addWindowContainer.window({
					title: battleServerManageAddWindowTitle,
					collapsible: false,
					minimizable: false,
					maximizable: false,
					closable: true,
					closed: true,
					modal: true,
				});
				Component.Combobox.Operator.create(
					BattleServer.Manage.AddWindow.operatorLabelContainer,
					BattleServer.Manage.AddWindow.operatorInputContainer
				);
				Component.Validatebox.AreaNum.create(
					BattleServer.Manage.AddWindow.areaNumLabelContainer,
					BattleServer.Manage.AddWindow.areaNumInputContainer
				);
				Component.Combobox.Server.create(
					BattleServer.Manage.AddWindow.serverLabelContainer,
					BattleServer.Manage.AddWindow.serverInputContainer,
					4
				);
				Component.Validatebox.PortGroup.create(
					BattleServer.Manage.AddWindow.portGroupLabelContainer,
					BattleServer.Manage.AddWindow.portGroupInputContainer
				);
				Component.Validatebox.DirectoryName.create(
					BattleServer.Manage.AddWindow.directoryNameLabelContainer,
					BattleServer.Manage.AddWindow.directoryNameInputContainer
				);
				Component.Combobox.Version.create(
					BattleServer.Manage.AddWindow.versionLabelContainer,
					BattleServer.Manage.AddWindow.versionInputContainer
				);
				BattleServer.Manage.AddWindow.submitButtonContainer.linkbutton({
					iconCls: 'icon-ok',
					text: battleServerManageAddWindowSubmitButtonText,
				});
				BattleServer.Manage.AddWindow.submitButtonContainer.bind(
					'click',
					BattleServer.Manage.AddWindow.doSubmit
				);
				BattleServer.Manage.AddWindow.resetButtonContainer.linkbutton({
					iconCls: 'icon-undo',
					text: battleServerManageAddWindowResetButtonText,
				});
				BattleServer.Manage.AddWindow.resetButtonContainer.bind(
					'click',
					BattleServer.Manage.AddWindow.doReset
				);
			},
			
			open: function() {
				BattleServer.Manage.AddWindow.reset();
				BattleServer.Manage.addWindowContainer.window('open');
			},
			
			close: function() {
				BattleServer.Manage.addWindowContainer.window('close');
			},
			
			doSubmit: function() {
				$.ajax({
					url: '/index.php/BattleServer/manage/add',
					data: {
						operatorId: BattleServer.Manage.AddWindow.operatorInputContainer.combobox('getValue'),
						areaNum: BattleServer.Manage.AddWindow.areaNumInputContainer[0].value,
						serverId: BattleServer.Manage.AddWindow.serverInputContainer.combobox('getValue'),
						portGroup: BattleServer.Manage.AddWindow.portGroupInputContainer[0].value,
						directoryName: BattleServer.Manage.AddWindow.directoryNameInputContainer[0].value,
						versionId: BattleServer.Manage.AddWindow.versionInputContainer.combobox('getValue')
					}
				})
				.done(function(response) {
					if(response.errorCode === 0) {
						$.messager.alert(messageWindowTitle, battleServerManageAddWindowSubmitSuccessInfo, 'info');
						BattleServer.Manage.AddWindow.close();
						BattleServer.Manage.View.DataGrid.load();
					}
				});
			},
			
			doReset: function() {
				BattleServer.Manage.AddWindow.reset();
			},
			
			reset: function() {
				BattleServer.Manage.AddWindow.operatorInputContainer.combobox('clear');
				BattleServer.Manage.AddWindow.areaNumInputContainer[0].value = '';
				BattleServer.Manage.AddWindow.serverInputContainer.combobox('clear');
				BattleServer.Manage.AddWindow.portGroupInputContainer[0].value = '';
				BattleServer.Manage.AddWindow.directoryNameInputContainer[0].value = '';
				BattleServer.Manage.AddWindow.versionInputContainer.combobox('clear');
			}
		},
	
		ModifyWindow: {
			operatorLabelContainer: '',
			operatorInputContainer: '',
			areaNumLabelContainer: '',
			areaNumInputContainer: '',
			serverLabelContainer: '',
			serverInputContainer: '',
			portGroupLabelContainer: '',
			portGroupInputContainer: '',
			directoryNameLabelContainer: '',
			directoryNameInputContainer: '',
			versionLabelContainer: '',
			versionInputContainer: '',
			statusLabelContainer: '',
			statusInputContainer: '',
			submitButtonContainer: '',
			resetButtonContainer: '',
			
			init: function() {
				BattleServer.Manage.modifyWindowContainer = $('#modifyWindow');
				BattleServer.Manage.ModifyWindow.operatorLabelContainer = $('#modifyWindow #operatorLabel');
				BattleServer.Manage.ModifyWindow.operatorInputContainer = $('#modifyWindow #operatorInput');
				BattleServer.Manage.ModifyWindow.areaNumLabelContainer = $('#modifyWindow #areaNumLabel');
				BattleServer.Manage.ModifyWindow.areaNumInputContainer = $('#modifyWindow #areaNumInput');
				BattleServer.Manage.ModifyWindow.serverLabelContainer = $('#modifyWindow #serverLabel');
				BattleServer.Manage.ModifyWindow.serverInputContainer = $('#modifyWindow #serverInput');
				BattleServer.Manage.ModifyWindow.portGroupLabelContainer = $('#modifyWindow #portGroupLabel');
				BattleServer.Manage.ModifyWindow.portGroupInputContainer = $('#modifyWindow #portGroupInput');
				BattleServer.Manage.ModifyWindow.directoryNameLabelContainer = $('#modifyWindow #directoryNameLabel');
				BattleServer.Manage.ModifyWindow.directoryNameInputContainer = $('#modifyWindow #directoryNameInput');
				BattleServer.Manage.ModifyWindow.versionLabelContainer = $('#modifyWindow #versionLabel');
				BattleServer.Manage.ModifyWindow.versionInputContainer = $('#modifyWindow #versionInput');
				BattleServer.Manage.ModifyWindow.statusLabelContainer = $('#modifyWindow #statusLabel');
				BattleServer.Manage.ModifyWindow.statusInputContainer = $('#modifyWindow #statusInput');
				BattleServer.Manage.ModifyWindow.submitButtonContainer = $('#modifyWindow #submitButton');
				BattleServer.Manage.ModifyWindow.resetButtonContainer = $('#modifyWindow #resetButton');
				BattleServer.Manage.ModifyWindow.create();
			},
			
			create: function() {
				BattleServer.Manage.modifyWindowContainer.window({
					title: battleServerManageModifyWindowTitle,
					collapsible: false,
					minimizable: false,
					maximizable: false,
					closable: true,
					closed: true,
					modal: true,
				});
				Component.Combobox.Operator.create(
					BattleServer.Manage.ModifyWindow.operatorLabelContainer,
					BattleServer.Manage.ModifyWindow.operatorInputContainer
				);
				Component.Validatebox.AreaNum.create(
					BattleServer.Manage.ModifyWindow.areaNumLabelContainer,
					BattleServer.Manage.ModifyWindow.areaNumInputContainer
				);
				Component.Combobox.Server.create(
					BattleServer.Manage.ModifyWindow.serverLabelContainer,
					BattleServer.Manage.ModifyWindow.serverInputContainer,
					4
				);
				Component.Validatebox.PortGroup.create(
					BattleServer.Manage.ModifyWindow.portGroupLabelContainer,
					BattleServer.Manage.ModifyWindow.portGroupInputContainer
				);
				Component.Validatebox.DirectoryName.create(
					BattleServer.Manage.ModifyWindow.directoryNameLabelContainer,
					BattleServer.Manage.ModifyWindow.directoryNameInputContainer
				);
				Component.Combobox.Version.create(
					BattleServer.Manage.ModifyWindow.versionLabelContainer,
					BattleServer.Manage.ModifyWindow.versionInputContainer
				);
				Component.Combobox.BattleServerStatus.create(
					BattleServer.Manage.ModifyWindow.statusLabelContainer,
					BattleServer.Manage.ModifyWindow.statusInputContainer
				);
				BattleServer.Manage.ModifyWindow.submitButtonContainer.linkbutton({
					iconCls: 'icon-ok',
					text: battleServerManageModifyWindowSubmitButtonText,
				});
				BattleServer.Manage.ModifyWindow.submitButtonContainer.bind(
					'click',
					BattleServer.Manage.ModifyWindow.doSubmit
				);
				BattleServer.Manage.ModifyWindow.resetButtonContainer.linkbutton({
					iconCls: 'icon-undo',
					text: battleServerManageModifyWindowResetButtonText,
				});
				BattleServer.Manage.ModifyWindow.resetButtonContainer.bind(
					'click',
					BattleServer.Manage.ModifyWindow.doReset
				);
			},
			
			open: function() {
				var checkedRow = BattleServer.Manage.View.DataGrid.getCheckedRows();
				
				if(checkedRow != false) {
					BattleServer.Manage.modifyWindowContainer.window('open');
					BattleServer.Manage.ModifyWindow.reset(checkedRow);
				}
			},
			
			close: function() {
				BattleServer.Manage.modifyWindowContainer.window('close');
			},
			
			doSubmit: function() {
				checkedRow = BattleServer.Manage.View.DataGrid.getCheckedRows();
				
				$.ajax({
					url: '/index.php/BattleServer/manage/modify',
					data: {
						battleServerId: checkedRow[0].battleServerId,
						operatorId: BattleServer.Manage.ModifyWindow.operatorInputContainer.combobox('getValue'),
						areaNum: BattleServer.Manage.ModifyWindow.areaNumInputContainer[0].value,
						serverId: BattleServer.Manage.ModifyWindow.serverInputContainer.combobox('getValue'),
						portGroup: BattleServer.Manage.ModifyWindow.portGroupInputContainer[0].value,
						directoryName: BattleServer.Manage.ModifyWindow.directoryNameInputContainer[0].value,
						versionId: BattleServer.Manage.ModifyWindow.versionInputContainer.combobox('getValue'),
						status: BattleServer.Manage.ModifyWindow.statusInputContainer.combobox('getValue')
					}
				})
				.done(function(response) {
					if(response.errorCode === 0) {
						$.messager.alert(messageWindowTitle, battleServerManageModifyWindowSubmitSuccessInfo, 'info');
						BattleServer.Manage.ModifyWindow.close();
						BattleServer.Manage.View.DataGrid.load();
					}
				});
			},
			
			doReset: function() {
				var checkedRow = BattleServer.Manage.View.DataGrid.getCheckedRows();
				
				if(checkedRow != false) {
					BattleServer.Manage.ModifyWindow.reset(checkedRow);
				}
			},
			
			reset: function(checkedRow) {
				BattleServer.Manage.ModifyWindow.operatorInputContainer.combobox('setValue', checkedRow[0].operatorId);
				BattleServer.Manage.ModifyWindow.areaNumInputContainer[0].value = checkedRow[0].areaNum;
				BattleServer.Manage.ModifyWindow.serverInputContainer.combobox('setValue', checkedRow[0].serverId);
				BattleServer.Manage.ModifyWindow.portGroupInputContainer[0].value = checkedRow[0].portGroup;
				BattleServer.Manage.ModifyWindow.directoryNameInputContainer[0].value = checkedRow[0].directoryName;
				BattleServer.Manage.ModifyWindow.versionInputContainer.combobox('setValue', checkedRow[0].versionId);
				BattleServer.Manage.ModifyWindow.statusInputContainer.combobox('setValue', checkedRow[0].status);
			},
		},
	},
}